package org.spiderflow.logs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.spiderflow.common.redis.userinfo.RedisUserInfoUtil;
import org.spiderflow.config.ResponseResult;
import org.spiderflow.config.RestResultGenerator;
import org.spiderflow.config.permission.PermissionCheck;
import org.spiderflow.config.permission.PermissionCodeEnum;
import org.spiderflow.core.model.DataSource;
import org.spiderflow.logs.entity.Logs;
import org.spiderflow.logs.service.LogsService;
import org.spiderflow.permission.entity.SysUserPermisson;
import org.spiderflow.users.entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Nicky.Tang on 2020/4/4 10:57 PM
 *
 * @since 02.12.10
 */

@RestController
@RequestMapping("logs")
@PermissionCheck(perCode = PermissionCodeEnum.rizhi)
public class LogsController {

    @Autowired
    LogsService logsService;
    @Autowired
    RedisUserInfoUtil redisUserInfoUtil;

    @RequestMapping(value = "/list")
    @ResponseBody
    public ResponseResult<IPage<Logs>> login(HttpServletRequest request,
                                             @RequestParam(name = "page", defaultValue = "1") Integer page,
                                             @RequestParam(name = "limit", defaultValue = "10") Integer size) throws Exception {
        Users usersRedis = redisUserInfoUtil.getUserByKey();
        if (usersRedis == null) {
            return RestResultGenerator.genErrorResult("您还未登陆！!");
        }
        QueryWrapper<Logs> queryWrapper = new QueryWrapper<>();
        if (!redisUserInfoUtil.checkPerOfAll(usersRedis, PermissionCodeEnum.rizhi)) {
            //权限范围不是【所有的】，只能查看当前登录用户的
            queryWrapper.eq("create_id", usersRedis.getUserId());
        }
        queryWrapper.orderByDesc("create_time");
//        int pageSize = logsService.count(queryWrapper);
        Page<Logs> res = new Page<Logs>(page, size);
        IPage<Logs> logs = logsService.page(res, queryWrapper);
        return RestResultGenerator.genResult(logs, "查询日志成功！");
    }

}
